#!usr/bin/python
# -*- coding:utf-8 -*-
#Copyright 2024 pysail

import os
import io
import sys
import threading
import shutil
from datetime import datetime

# 设置控制台输出编码为UTF-8
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

file_name = f'amzkindasin_{datetime.now().strftime("%Y%m%d")}'
# 设置源文件夹和目标文件夹
# file_name = f'amzkindupdata_20241118'
source_folder = r'E:\amazon_project\%s'%file_name

# 获取源文件夹中所有文件，假设有5个文件
files = [f for f in os.listdir(source_folder) if os.path.isfile(os.path.join(source_folder, f))]
target_folders =[str(i) for i in range(1, len(files)+1)] if len(files)<=10 else [str(i) for i in range(1, 11)]

# 确保目标文件夹存在
for folder in target_folders:
    os.makedirs(os.path.join(source_folder, folder), exist_ok=True)

groups = [files[i::len(target_folders)] for i in range(len(target_folders))]

for i, group in enumerate(groups):
    for file in group:
        shutil.move(os.path.join(source_folder, file), os.path.join(source_folder, target_folders[i]))
        shutil.move(os.path.join(source_folder, file[:file.index('.')]), os.path.join(source_folder, target_folders[i]))

#
# files = [f for f in os.listdir(source_folder)]
# target_folders =[str(i) for i in range(1, len(files)+1)] if len(files)<=10 else 10
def thread_function(name):
    os.system(r'cd .. && python sail.py spider --kind amazon --plug amazonasin -workpath %s\%s'% (source_folder,name))

# 创建线程
for i in range(len(target_folders)):
    thread = threading.Thread(target=thread_function, args=(i+1,))
# 启动线程
    thread.start()